#include "wir.h"
#include "rectype.h"
#include "util.h"
#include "nmfield.h"
#include "cnfield.h"

using namespace std;
using namespace opsrc::stdf;

Wir::Wir()
{
}

Wir::Wir(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf)
:RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf!=0)
        readFields(buf);
}


Wir::~Wir() {
}

const StdfField<int> Wir::getHeadNum() const {
    return head_num;
}


void Wir::setHeadNum(StdfField<int> headnum) {
    head_num = headnum;
}

const StdfField<int> Wir::getSiteGroup() const {
    return site_grp;
}

void Wir::setSiteGroup(StdfField<int> sitegrp) {
    site_grp = sitegrp;
}

const StdfField<long> Wir::getStartTime() const {
    return start_t;
}

void Wir::setStartTime(StdfField<long> startt) {
    start_t = startt;
}

const StdfField<string> Wir::getWaferId() const {
    return wafer_id;
}

void Wir::setWaferId(StdfField<string> waferid) {
    wafer_id = waferid;
}

void Wir::readFields(char *buf) {
#if _DEBUG
    cout << "stdfrec= WIR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read head number , 1 byte
    NmField<unsigned int, 1> headnum(buf+curpos, en);
    head_num.set(headnum.getData(), curpos, headnum.getByteCount(), headnum.getTotalByteCount());
    curpos += head_num.getTotalByteCount();


    // Read site group , 1 byte
    if (curpos < reclen-1) {
        NmField<unsigned int, 1> sitegrp(buf+curpos, en);
        site_grp.set(sitegrp.getData(), curpos, sitegrp.getByteCount(), sitegrp.getTotalByteCount());
        curpos += site_grp.getTotalByteCount();
    }



    // Read site group , 1 byte
    if (curpos < reclen-1) {
        NmField<unsigned long, 4> startt(buf+curpos, en);
        start_t.set(startt.getData(), curpos, startt.getByteCount(), startt.getTotalByteCount());
        curpos += start_t.getTotalByteCount();
    }


    // Read wafer id ,string
    if (curpos < reclen-1) {
        CnField<string> waferid(buf+curpos, en);
        wafer_id.set(waferid.getData(), curpos, waferid.getByteCount(), waferid.getTotalByteCount());
        curpos += waferid.getTotalByteCount();
    }
}
